package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/13 11:17
 */
public class C622 {

    static class MyCircularQueue {

        int head, tail, size, maxCapacity;
        int[] elements;

        public MyCircularQueue(int k) {
            elements = new int[k];
            maxCapacity = k;
        }

        public boolean enQueue(int value) {
            if (size == maxCapacity)
                return false;
            if (tail < maxCapacity)
                elements[tail] = value;
            else
                elements[(tail = 0)] = value;
            tail++;
            size++;
            return true;
        }

        public boolean deQueue() {
            if (size < 1)
                return false;
            if (head < maxCapacity) {
                head++;
            } else {
                head = 0;
            }
            size--;
            return true;
        }

        public int Front() {
            if (size < 1)
                return -1;
            return elements[head];
        }

        public int Rear() {
            if (size < 1)
                return -1;
            return elements[tail - 1];
        }

        public boolean isEmpty() {
            return size < 1;
        }

        public boolean isFull() {
            return size == maxCapacity;
        }
    }


    // for test
    public static void main(String[] args) {

        // ["MyCircularQueue","enQueue","enQueue","enQueue","enQueue","deQueue","deQueue","isEmpty","isEmpty","Rear","Rear","deQueue"]
        //[[8],[3],[9],[5],[0],[],[],[],[],[],[],[]]

        MyCircularQueue queue = new MyCircularQueue(2);
        System.out.println(queue.enQueue(1));
        System.out.println(queue.enQueue(2));
        System.out.println(queue.deQueue());
        System.out.println(queue.enQueue(3));
        System.out.println(queue.deQueue());
        System.out.println(queue.enQueue(3));
        System.out.println(queue.deQueue());
        System.out.println(queue.enQueue(3));
        System.out.println(queue.deQueue());
        System.out.println(queue.Front());
    }

}
